// import type { CommonUploadFile } from './commonTypes'
import { service } from '@/service'

export const uploadCommon = (formData: FormData) => {
  return service.request({
    url: 'hbsys-system/minio/upload',
    method: 'POST',
    data: formData
  })
  // return new Promise<Service.Result<CommonUploadFile>>((resolve) => {
  //   setTimeout(() => {
  //     const bold = formData.get('file')
  //     if (bold && typeof bold !== 'string') {
  //       resolve({
  //         data: {
  //           url: URL.createObjectURL(bold),
  //           filename: bold.name,
  //           mimeType: bold.type
  //         }
  //       })
  //     }
  //   }, 500)
  // })
}

export function downloadBlob(config, fileName = '') {
  return service
    .requestNotHandle({
      ...config,
      responseType: 'blob'
    })
    .then((res) => {
      let resFileName = ''
      try {
        resFileName = decodeURIComponent(
          res.headers['content-disposition'].split('=')[1]
        )
      } catch (error) {}

      const url = window.URL.createObjectURL(res.data)
      const aEl = document.createElement('a')
      aEl.target = '_blank'
      aEl.href = url

      resFileName = resFileName || fileName
      if (resFileName) {
        aEl.download = resFileName
      }
      aEl.click()
      return {
        res,
        url
      }
    })
}

export * from './commonTypes'
